<?php
namespace App\Controller\Index;

use Common\Controller\App\SignController;
use Common\Model\ActivityModel;
use Common\Model\ActivityProductListModel;
use Common\Model\AdvsConModel;
use Common\Model\BrandCatModel;
use Common\Model\BrandModel;
use Common\Model\CaptchaModel;
use Common\Model\ProductSpikeModel;
use Think\Wxpay\Wxpay;

class IndexController extends SignController{


    /**
     * Introduction: 生成二维码
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    public function qr()
    {

        $text = I('get.text');

        $size = I('get.size',4);
        $qr = new \Think\QR($text);

        header("content-type: image/png");
        echo $qr->image($size);

    }
    /**
     * 获取微信获取jsApi
     */

    public function getJsSdk(){
        $wxpay = new Wxpay();
        $info = $wxpay->getJsSdk();
        $this->ajaxReturn(['code'=>200,'info'=>$info]);
    }

    /**
     * 搜索页面接口
     */
    public function search(){
        $model = new BrandCatModel();
        $guojia_list = $model->getlist();
        $model = new BrandModel();
        $page = I('post.page')?I('post.page'):1;
        $limit = I('post.limit')?I('post.limit'):200;
        $num = ($page-1)*$limit;
        $brand_list = $model->getlist($num,$limit);
        $this->ajaxReturn(['code'=>'200','guojia_list'=>$guojia_list,'brand_list'=>$brand_list]);
    }

    /**
     * 首页默认接口
     */
    public function index(){
        $model = new AdvsConModel();
        $banner_list = $model->getRow(47);
        $model = new ProductSpikeModel();
        $mslist = $model->getList();
        $model = new ActivityModel();
        $page = I('post.page')?I('post.page'):1;
        $limit = I('post.limit')?I('post.limit'):20;
        $num = ($page-1)*$limit;
        $h_list = $model->getlist(1,$num,$limit); //红人推荐
        $j_list = $model->getlist(2,$num,$limit); //今日上新
        $this->ajaxReturn(['code'=>200,'banner_list'=>$banner_list,'ms_list'=>$mslist,'h_list'=>$h_list,'j_list'=>$j_list]);
    }


    /**
     * 首页今日新品与红人推荐商品列表
     */
    public function goods_list(){
        $id = I('post.id');
        if(empty($id)){
            $this->ajaxReturn(['code'=>300,'msg'=>'活动编号错误']);
        }
        $model = new ActivityProductListModel();
        $res = $model->getlist($id);
        $this->ajaxReturn(['code'=>200,'res'=>$res]);
    }
    public function send(){
        $mobile = I('post.mobile');
        $code = rand(1000,9999);
        $model = new CaptchaModel();
        $datas['unique_str'] = $mobile;
        $datas['captcha'] = $code;
        $datas['invalid_time'] = date('Y-m-d H:i:s',time()+300);
        $datas['type']   = 'normal';
        $datas['create_time']   = date('Y-m-d H:i:s',time());
        $model->add($datas);
        $svr_url = 'http://112.74.76.186:8030/service/httpService/httpInterface.do';   // 服务器接口路径
        $svr_param = array();
        $svr_param['username'] = 'JSM40969';    // 账号
        $svr_param['password'] = 's6023s07';    // 密码
        $svr_param['veryCode'] = '79sgvkjul2am';    // 通讯认证Key
        $post_data = $svr_param;
        $post_data['method'] = 'sendMsg';
        $post_data['mobile'] = $mobile;
        $post_data['tempid'] = 'JSM40969-0001';
        $post_data['content']= '@1@='.$code;
        $post_data['msgtype']= '2';       // 1-普通短信，2-模板短信
        $post_data['code']   = 'utf-8';   // utf-8,gbk

        //验证码加入数据库

        $res = $this->request_post($svr_url, $post_data);  // 如果账号开了免审，或者是做模板短信，将会按照规则正常发出，而不会进人工审核平台
        $this->echo_xmlarr($res);
    }
    /**
     * 模拟post进行url请求
     * @param string $url
     * @param array $post_data
     */
    public function request_post($url = '', $post_data = array()) {
        if (empty($url) || empty($post_data)) {
            return false;
        }

        $o = "";
        foreach ( $post_data as $k => $v )
        {
            $o.= "$k=" . urlencode( $v ). "&" ;
        }
        $post_data = substr($o,0,-1);

        $postUrl = $url;
        $curlPost = $post_data;
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);

        return $data;
    }
    // 页面显示数组格式，用于调试
    public function echo_xmlarr($res) {
        $res = $this->xml_to_array($res);
        if($res['sms']['mt']['status']==0){
            $this->ajaxReturn(['code'=>'200','msg'=>'手机验证码发送成功']);
        }else{
            $this->ajaxReturn(['code'=>'200','msg'=>'手机验证码发送失败']);

        }
       // echo "<pre>";
       // print_r($res);
       // echo "</pre>";
    }

// XML格式转数组格式
    public function xml_to_array( $xml ) {
        $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
        if(preg_match_all($reg, $xml, $matches)) {
            $count = count($matches[0]);
            for($i = 0; $i < $count; $i++) {
                $subxml= $matches[2][$i];
                $key = $matches[1][$i];
                if(preg_match( $reg, $subxml )) {
                    $arr[$key] = $this->xml_to_array( $subxml );
                } else {
                    $arr[$key] = $subxml;
                }
            }
        }
        return $arr;
    }
}